<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <script src="../js/jquery.min.js"></script>
</head>
<body>
    <script>
        console.log("-------2.32rest参数------");
        //es5方式
        function join(){
            var str = "";
            for(var i = 0;i < arguments.length;i++){
                if(i == 0){
                    str = arguments[i];
                }else{
                    str += "|" + arguments[i];
                }
            }
            return str;
        }
 
        console.log(join("hello","world"));

        //es6方式
        function join2(...str2){
            var str = "";
            for(var i = 0;i < str2.length;i++){
                if(i == 0){
                    str = str2[i];
                }else{
                    str += "|" + str2[i];
                }
            }
            return str;
        }
        console.log(join2("hello","world"));

        console.log("======rest参数应用-计算器=======");
        function calculate(method,...nums){
            let result;
            if(!method||nums.length == 0){
                alert('请输入参数!');
                return;
            }
            
            if(method == '*'){
                result = 1;
                for(let i =0;i < nums.length;i++){
                    result *= nums[i];
                }
            }else if(method == '+'){
                result = 0;
                for(let i = 0;i < nums.length;i++){
                    result += nums[i];
                }
            }
            alert(result);
        }

        calculate("+",1,2);
        calculate("+",1,2,3,4,5);
        calculate("*",1,2,3);
        calculate("*",1,2,3,4,5);



    </script> 
</body>
</html>